import phaser from 'phaser';
import PlayerBase from '../players/PlayerBase';
import configer from '../types/configer';
import Joystick from '../types/Joystick';
import Director from '../players/Director';

/** 仪表盘 */
export default class Dashboard extends phaser.Scene {
    public joystick: Joystick = null!;
    public rxCount: number = 0;
    private gameWidth: number = 0;
    private gameHeight: number = 0;
    private director: Director = null!;
    private attckContainer: Phaser.GameObjects.Container = null!;
    private attck1Container: Phaser.GameObjects.Container = null!;
    private attck2Container: Phaser.GameObjects.Container = null!;

    // private baseInfo: Phaser.GameObjects.Container = null!;
    private baseInfo: Phaser.GameObjects.Text = null!;
    private cursors: Phaser.Types.Input.Keyboard.CursorKeys = null!;

    // private updateContainer: Phaser.GameObjects.Container = null!;
    constructor() {
        super('Dashboard');
    }

    init(data: any) {
        console.log('Dashboard init', data);
        if ('scene' in data) {
            this.director = <Director>data.scene.director;
        }
        this.scene.bringToTop();

        this.gameWidth = <number>this.sys.game.config.width;
        this.gameHeight = <number>this.sys.game.config.height;
    }

    send(action: string, data: any = null) {
        const timestamp = new Date().getTime();
        if (window.client.stateConnect == true) {
            window.client.sendAction('Moba2D', action, data);
        }
    }

    isCtrl() {
        if (this.director.playerMain.stateDead) return false;
        if (this.director.playerMain.stateShihua) return false;
        if (this.director.playerMain.serverInfo.isActionAttack) return false;
        if (this.director.playerMain.serverInfo.isActionSkillOne) return false;
        if (this.director.playerMain.serverInfo.isActionSkillTwo) return false;
        return true;
    }

    /** 初始化摇杆 */
    initJoystick(x: number, y: number, width: number, height: number) {
        this.joystick = new Joystick(this, x, y, width, height);
        this.joystick.onUp(() => {
            if (!this.director.playerMain.serverInfo.isMoveUp && this.isCtrl()) {
                this.send('moveUp');
                this.director.playerMain.serverInfo.isMoveUp = true
            }
            // this.director.playerMain.moveUp();
        });
        this.joystick.onDown(() => {
            // this.director.playerMain.moveDown();
            if (!this.director.playerMain.serverInfo.isMoveDown && this.isCtrl()) {
                this.send('moveDown');
                this.director.playerMain.serverInfo.isMoveDown = true
            }
        });
        this.joystick.onLeft(() => {
            // this.director.playerMain.moveLeft();
            if (!this.director.playerMain.serverInfo.isMoveLeft && this.isCtrl()) {
                this.send('moveLeft');
                this.director.playerMain.serverInfo.isMoveLeft = true
            }
        });
        this.joystick.onRight(() => {
            // this.director.playerMain.moveRight();
            if (!this.director.playerMain.serverInfo.isMoveRight && this.isCtrl()) {
                this.send('moveRight');
                this.director.playerMain.serverInfo.isMoveRight = true
            }
        });
        this.joystick.onMiddle(() => {
            if (this.isCtrl()) {
                this.send('stopMove');
            }
        });

        this.cursors = this.director.scene.input.keyboard.createCursorKeys();

        this.cursors.up!.on('down', () => {
            if (!this.director.playerMain.serverInfo.isMoveUp && this.isCtrl()) {
                this.send('moveUp');
                this.director.playerMain.serverInfo.isMoveUp = true
            }
        });
        this.cursors.down!.on('down', () => {
            if (!this.director.playerMain.serverInfo.isMoveDown && this.isCtrl()) {
                this.send('moveDown');
                this.director.playerMain.serverInfo.isMoveDown = true
            }
        });
        this.cursors.left!.on('down', () => {
            if (!this.director.playerMain.serverInfo.isMoveLeft && this.isCtrl()) {
                this.send('moveLeft');
                this.director.playerMain.serverInfo.isMoveLeft = true
            }
        })
        this.cursors.right!.on('down', () => {
            if (!this.director.playerMain.serverInfo.isMoveRight && this.isCtrl()) {
                this.send('moveRight');
                this.director.playerMain.serverInfo.isMoveRight = true
            }
        });

    }


    addButton(x: number, y: number, w: number, h: number, text: string = ''): Phaser.GameObjects.Container {
        let button: Phaser.GameObjects.Container = this.add.container(x, y, [
            this.add.graphics().fillStyle(0xFFFFFF, 0.8).fillEllipse(0, 0, w, h),
            this.add.text(-10, 0, text, { fontSize: '10px', color: '#f00' })
        ]).setSize(w, h).setInteractive();
        return button;
    }

    initAttackButton() {

        this.baseInfo = this.add.text(this.gameWidth - 150, 10, 'X:0 Y:0 fps:0', { fontSize: '12px', color: 'yellow' })

        // this.add.container(this.gameWidth - 100, 10, [
        // ]);

        this.attckContainer = this.addButton(this.gameWidth - 100, this.gameHeight - 100, 80, 80, 'A');
        this.attck1Container = this.addButton(this.gameWidth - 100, this.gameHeight - 200, 50, 50, 'Q');
        this.attck2Container = this.addButton(this.gameWidth - 100, this.gameHeight - 300, 50, 50, 'W');

        // this.updateContainer = this.addButton(100, 100, 50, 50);

        this.input.on('gameobjectdown', (pointer: any, gameObject: Phaser.GameObjects.Container) => {
            if (gameObject == this.attckContainer) {
                // this.director.playerMain.setActionAttack();
                if (this.isCtrl()) {
                    this.send('actionAttack');
                }
            }
            if (gameObject == this.attck1Container) {
                // this.director.playerMain.setActionSkillOne();
                if (this.isCtrl()) {
                    this.send('actionSkillOne');
                }
            }
            if (gameObject == this.attck2Container) {
                // this.director.playerMain.setActionSkillTwo();
                if (this.isCtrl()) {
                    this.send('actionSkillTwo');
                }
            }
            // if (gameObject == this.updateContainer) {
            //     this.director.switchPlayer();
            // }
        });


        // var keyObjectA = this.director.scene.input.keyboard.addKey();



        var keyObjectA = this.director.scene.input.keyboard.addKey("A");
        keyObjectA.on('down', () => {
            // this.send('actionAttack');
            if (this.isCtrl()) {
                this.send('actionAttack');
            }
        });
        var keyObjectQ = this.director.scene.input.keyboard.addKey("Q");
        keyObjectQ.on('down', () => {
            // this.send('actionAttack');
            if (this.isCtrl()) {
                this.send('actionSkillOne');
            }
        });
        var keyObjectW = this.director.scene.input.keyboard.addKey("W");
        keyObjectW.on('down', () => {
            // this.send('actionAttack');
            if (this.isCtrl()) {
                this.send('actionSkillTwo');
            }
        });

        var keyObjectW = this.director.scene.input.keyboard.addKey("S");
        keyObjectW.on('down', () => {
            // this.send('actionAttack');
            this.send('stopMove');
        });


    }


    create() {

        this.input.addPointer(2);
        /** 初始化摇杆 */
        this.initJoystick(configer.DIRECTION_HANDLE_WIDTH / 2 + 30,
            this.gameHeight - (configer.DIRECTION_HANDLE_HEIGHT / 2),
            configer.DIRECTION_HANDLE_WIDTH,
            configer.DIRECTION_HANDLE_HEIGHT);
        /** 创建技能按键 */
        this.initAttackButton();
    }

    update(time: number, delta: number): void {
        if (this.director && this.director.playerMain) {
            this.baseInfo.setText('X:' + parseInt(this.director.playerMain.spine.x + '') + ' Y:' + parseInt(this.director.playerMain.spine.y + '') + ' RX:' + this.director.rxCount);
        }
        if (this.attck1Container && this.attck2Container && this.director.playerMain && this.director.playerMain.serverInfo && this.director.playerMain.serverInfo.role) {
            if (this.director.playerMain.serverInfo.role.skillOne.cdTime > 0) {

                let rem = this.director.playerMain.serverInfo.role.skillOne.cd - this.director.playerMain.serverInfo.role.skillOne.cdTime;
                let r = rem / this.director.playerMain.serverInfo.role.skillOne.cd;
                let but = <Phaser.GameObjects.Graphics>this.attck1Container.list[0];
                but.setAlpha(r);
                if (this.attck1Container.list[1]) {
                    let text: Phaser.GameObjects.Text = <Phaser.GameObjects.Text>this.attck1Container.list[1];
                    if (this.director.playerMain.serverInfo.role.skillOne.cdTime > 0.1) {
                        text.setText('' + this.director.playerMain.serverInfo.role.skillOne.cdTime.toFixed(1));
                        text.x = -4;
                    } else {
                        text.setText('Q');
                        text.x = 0;
                    }
                }
            }
            if (this.director.playerMain.serverInfo.role.skillTwo.cdTime > 0) {
                let rem = this.director.playerMain.serverInfo.role.skillTwo.cd - this.director.playerMain.serverInfo.role.skillTwo.cdTime;
                let r = rem / this.director.playerMain.serverInfo.role.skillTwo.cd;
                let but = <Phaser.GameObjects.Graphics>this.attck2Container.list[0];
                but.setAlpha(r);
                if (this.attck2Container.list[1]) {
                    let text: Phaser.GameObjects.Text = <Phaser.GameObjects.Text>this.attck2Container.list[1];
                    if (this.director.playerMain.serverInfo.role.skillTwo.cdTime > 0.1) {
                        text.setText('' + this.director.playerMain.serverInfo.role.skillTwo.cdTime.toFixed(1));
                        text.x = -4;
                    } else {
                        text.setText('W');
                        text.x = 0;
                    }
                }
            }
        }

        // if (this.cursors.up!.isDown) {
        //     if (!this.director.playerMain.serverInfo.isMoveUp && this.isCtrl()) {
        //         this.send('moveUp');
        //     }
        // }
        // else
        //     if (this.cursors.down!.isDown) {
        //         if (!this.director.playerMain.serverInfo.isMoveDown && this.isCtrl()) {
        //             this.send('moveDown');
        //         }
        //     }

        // if (this.cursors.left!.isDown) {
        //     if (!this.director.playerMain.serverInfo.isMoveLeft && this.isCtrl()) {
        //         this.send('moveLeft');
        //     }
        // }
        // else
        //     if (this.cursors.right!.isDown) {
        //         if (!this.director.playerMain.serverInfo.isMoveRight && this.isCtrl()) {
        //             this.send('moveRight');
        //         }
        //     }



    }
}
